package com.ibeetl.member.service;


import com.ibeetl.admin.core.conf.PasswordConfig.PasswordEncryptService;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;
import com.ibeetl.member.dao.MemberDao;
import com.ibeetl.member.entity.Member;
import com.ibeetl.member.rbac.MemberLoginInfo;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;






/**
 * 培训会员 Service
 */

@Service
@Transactional
public class MemberService extends CoreBaseService<Member>{

	@Autowired
	PasswordEncryptService passwordEncryptService;
	
	@Autowired SQLManager sqlManager;
	
	@Autowired
    FileService fileService;
	
    @Autowired private MemberDao memberDao;

	public Member getMemberByLoginName(String loginName) {
		if (StringUtils.isNotBlank(loginName)) {
			Member template = new Member();
			template.setLoginName(loginName);
			Member member = memberDao.templateOne(template);
			return member;
		} else {
			return null;
		}

	}

	public Member getMemberByDomain(String domain) {
		if (StringUtils.isNotBlank(domain)) {
			Member template = new Member();
			template.setDomain(domain);
			Member member = memberDao.templateOne(template);
			return member;
		} else {
			return null;
		}

	}

    public Member getMemberById(Integer memberId){
		return memberDao.unique(memberId);
	}
    
    public PageQuery<Member>queryByCondition(PageQuery query){
        PageQuery ret =  memberDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelMember(List<Long> ids){
        try {
            memberDao.batchDelMemberByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除培训会员失败", e);
        }
    }

    public MemberLoginInfo loginByPhone(String phone, String password){
		Member query = new Member();
		query.setPhone(phone);
		query.setPassword(passwordEncryptService.password(password));
		//query.setState(GeneralStateEnum.ENABLE.getValue());
		Member member =memberDao.createLambdaQuery().andEq(Member::getPhone,phone).andEq(Member::getPassword, passwordEncryptService.password(password)).single();
		if(member==null) {
			return null;
		    //throw new PlatformException("用户"+userName+"不存在或者密码错误");
		} 
		MemberLoginInfo loginInfo = new MemberLoginInfo();
		loginInfo.setMember(member);
		return loginInfo;
		
	}
	
	/**
	 * 重置用户密码
	 *
	 * @param id
	 * @param password
	 */
	public int resetPassword(Integer id, String password) {
		Member member = this.queryById(id);
		member.setPassword(passwordEncryptService.password(password));
		return memberDao.updateTemplateById(member);
	}

	public int resetPassword(Member member, String password) {
		member = this.queryById(member.getId());
		member.setPassword(passwordEncryptService.password(password));
		return memberDao.updateTemplateById(member);
	}
	
	/**
	 * 更新用户 只更新不为空的字段
	 *
	 * @param user
	 * @return
	 */
	public int updateMember(Member user) {
		return memberDao.updateTemplateById(user);
	}
	
	/**
	 * 插入一条用户数据
	 *
	 * @param member
	 */
	public Member addMember(Member member) {
		Member query = new Member();
		query.setPhone(member.getPhone());
		Member dbMember = memberDao.templateOne(query);
		if (dbMember != null) {
			throw new PlatformException("保存用户信息失败,手机号码已经存在");
		}
		member.setCreateTime(new Date());
		member.setState(GeneralStateEnum.ENABLE.getValue());
		member.setPassword(passwordEncryptService.password(member.getPassword()));
		memberDao.insert(member, true);
		if(StringUtils.isNotEmpty(member.getAttachmentId())){
		    //更新附件详细信息,关联到这个用户
		    fileService.updateFile(member.getAttachmentId(), Member.class.getSimpleName(), String.valueOf(member.getId()));
		}
		

		return member;
	}

	public MemberLoginInfo login(String loginName, String password){
		Member query = new Member();
		query.setLoginName(loginName);
		query.setPassword(passwordEncryptService.password(password));
		query.setState(GeneralStateEnum.ENABLE.getValue());
		Member member = memberDao.createLambdaQuery().andEq(Member::getLoginName,loginName).
				andEq(Member::getPassword, passwordEncryptService.password(password)).orderBy("id").single();
		if(member==null) {
			throw new PlatformException("用户"+loginName+"不存在或者密码错误");
		}

		MemberLoginInfo loginInfo = new MemberLoginInfo();
		loginInfo.setMember(member);
		return loginInfo;

	}
	
	/**
	 * 根据条件查询
	 *
	 * @param query
	 */
	public void queryByCondtion(PageQuery<Member> query) {
		PageQuery<Member> ret = memberDao.queryByCondition(query);
		queryListAfter(ret.getList());
	}

	public void removeTopBg(Integer memberId) {
		this.memberDao.removeTopBg(memberId);
	}
	public void updateTopBg(String bgId, Integer memberId) {
		this.memberDao.updateTopBg(bgId, memberId);
	}
}